package com.pospi.xstore.view.PointSelectUtil;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.pospi.xstore.R;
import com.pospi.xstore.dto.GoodsDto;
import com.pospi.xstore.util.Api;

import java.text.DecimalFormat;
import java.util.List;

public class TestSectionedAdapter extends SectionedBaseAdapter {

    private Context mContext;
    private List<String> leftStr;
    private List<List<GoodsDto>> lists;

    public TestSectionedAdapter(Context context, List<String> leftStr, List<List<GoodsDto>> lists) {
        this.mContext = context;
        this.leftStr = leftStr;
        this.lists = lists;
    }

    @Override
    public Object getItem(int section, int position) {
        return lists.get(section).get(position);
    }

    @Override
    public long getItemId(int section, int position) {
        return position;
    }

    @Override
    public int getSectionCount() {
        return leftStr.size();
    }

    @Override
    public int getCountForSection(int section) {
        return lists.get(section).size();
    }

    @Override
    public View getItemView(final int section, final int position, View convertView, ViewGroup parent) {
        LinearLayout layout = null;
        if (convertView == null) {
            LayoutInflater inflator = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layout = (LinearLayout) inflator.inflate(R.layout.list_item, null);
        } else {
            layout = (LinearLayout) convertView;
        }
        ((TextView) layout.findViewById(R.id.textItem)).setText(lists.get(section).get(position).getName());
        ImageView iv = (ImageView) layout.findViewById(R.id.showview);
        TextView tv_spec = layout.findViewById(R.id.tv_spec);
        tv_spec.setText(lists.get(section).get(position).getSpec()+lists.get(section).get(position).getUnit());

//        if (lists.get(section).get(position).getImage()!=null){
//            String url= Api.HOST+lists.get(section).get(position).getImage();
//            iv.setImageBitmap(getHttpBitmap(url));
//        }
        DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
        ((TextView) layout.findViewById(R.id.money)).setText(decimalFormat.format(lists.get(section).get(position).getPrice()));

        Glide.with(mContext).load(Api.HOST + "/"+lists.get(section).get(position).getImage()).placeholder(R.drawable.noimageshow).dontAnimate().into(iv);
//        layout.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View arg0) {
//                Toast.makeText(mContext, lists.get(section).get(position).getName(), Toast.LENGTH_SHORT).show();
//            }
//        });
        return layout;
    }

    @Override
    public View getSectionHeaderView(int section, View convertView, ViewGroup parent) {
        LinearLayout layout = null;
        if (convertView == null) {
            LayoutInflater inflator = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layout = (LinearLayout) inflator.inflate(R.layout.header_item, null);
        } else {
            layout = (LinearLayout) convertView;
        }
        layout.setClickable(false);
        ((TextView) layout.findViewById(R.id.textItem)).setText(leftStr.get(section));
        return layout;
    }

}
